import Vue from 'vue'
import Vuex from 'vuex'
// 导入localStorage操作方法
import {
  getLocalStorageItem,
  setLocalStorageItem
} from '@/utils/storage'

Vue.use(Vuex)
const TOKEN_KEY = 'USER_TOKEN'
const USER = 'USER_INFO'
export default new Vuex.Store({
  state: {
    userDataServe: getLocalStorageItem(TOKEN_KEY),
    currentUser: getLocalStorageItem(USER) || {}
  },
  mutations: {
    // 用户登录设置token,
    // 使用Vux仓库于localStorage结合,实现持久化
    setUser (state, data) {
      state.userDataServe = data
      setLocalStorageItem(TOKEN_KEY, data)
    },
    // 设置当前用户信息
    setUserInfo (state, data) {
      state.currentUser = data
      setLocalStorageItem(USER, data)
    }
  },
  actions: {
  },
  modules: {
  }
})
